package com.liuche.pictureserver.process.picture;

import com.liuche.pictureserver.pojo.dto.PictureSaveDTO;
import com.liuche.pictureserver.process.ProcessContext;
import com.liuche.pictureserver.process.ProcessHandler;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

/**
 * 图片信息填充
 *
 * @author liuche
 */
@Slf4j
@Component
public class PictureInfoFillProcess extends ProcessHandler {

    /**
     * 处理逻辑
     *
     * @param context 上下文信息
     */
    @Override
    @SneakyThrows
    public void run(ProcessContext<?> context) {
        PictureSaveDTO model = (PictureSaveDTO) context.getModel();
        MultipartFile file = model.getFile();
        BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
        model.setPicWidth(bufferedImage.getWidth());
        model.setPicHeight(bufferedImage.getHeight());
        model.setPicScale(Double.parseDouble(String.format("%.2f", (double) model.getPicWidth() / model.getPicHeight())));
        model.setPicFormat(file.getContentType());
        model.setPicSize(file.getSize());
    }
}
